package com.example.jsf.controller;

import com.example.jsf.Repository.Memberepository;
import com.example.jsf.entity.Income;
import com.example.jsf.entity.Member;
import com.example.jsf.other.DataHelper;
import com.example.jsf.other.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("member")
public class MemberController {

    @Autowired
    private Memberepository memberepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping("addMember")
    public Result addMember(@RequestBody Member member) {
        if(member.getId() == null){
            member.setCreateTime(new Date());
        }
        return  Result.success(memberepository.save(member));
    }

    @GetMapping("getMemberList")
    public Result getMember(
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize",defaultValue = "2") int pageSize,
            String code,
            String userName,
            String mobile
    ) {
        String s = DataHelper.selectSQL(Member.class);
        s+="where id is not null ";
        if(StringUtils.hasText(code)){
            s+="and code like '%"+ code+"%' ";
        }
        if(StringUtils.hasText(userName)){
            s+="and user_name like '%"+ userName+"%' ";
        }
        if(StringUtils.hasText(mobile)){
            s+="and mobile like '%"+ mobile+"%' ";
        }

        s+=" order by create_time desc";

        System.out.println("s = " + s);
        return Result.success(DataHelper.pageQuery(jdbcTemplate,s,pageNo,pageSize,Member.class));

    }
}
